<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Audi extends Controller_Base
{
	private $per_page = 14;

	public function before()
	{
		parent::before();
	}	
		
	public function action_index()
	{
		$types_of_body = $this->audi->get_types_of_body();
		$add_title = 'Модельный ряд - ';
		
		$categories = $this->audi->get_categories();
		$view = new View('index_tpl');
		$view->categories = $categories;
				
		$this->print_template($add_title, $view);
	}
	
	public function action_get_model_by_name($model)
	{	
		$this->action_get_model_by_name_and_page($model, 1);
	}
	
	public function action_get_model_by_name_and_page($model, $page)
	{
		$start = ($page-1) * $this->per_page;
		$count = $this->per_page;
		
		$category_id   = $this->audi->get_category_id($model);
	
		$subcategories  = $this->audi->get_generations($category_id);
		$images		    = $this->audi->get_images($category_id, $start, $count);
		$description    = $this->audi->get_description($category_id);
		$pages		    = $this->audi->get_pages($category_id, $count, $page);
		
		if($page == 1)
			$add_title	  = 'Audi '.strtoupper($model).' - ';
		else
			$add_title	  = 'Audi '.strtoupper($model).' - Страница '.$page.' - ';
		
		$view 		  = new View('model_tpl');
		
		$view->subcategories = $subcategories;
		$view->images = $images;
		$view->model  = $model;
		$view->page   = $page;
		$view->description = $description;
		$view->pages  = $pages;
		
		$this->print_template($add_title, $view);
	}
	
	public function action_get_generation_by_name($model, $generation)
	{	
		$this->action_get_generation_by_name_and_page($model, $generation, 1);
	}
	
	public function action_get_generation_by_name_and_page($model, $generation, $page)
	{	
		$start = ($page-1) * $this->per_page;
		$count = $this->per_page;
		
		$category_id   = $this->audi->get_subcategory_id($model, $generation);
	
		$images		   = $this->audi->get_images($category_id, $start, $count);
		$description   = $this->audi->get_description($category_id);
		$pages		   = $this->audi->get_pages($category_id, $count, $page);
	
		if($page == 1)
			$add_title	  = 'Audi '.strtoupper($model).' '.strtoupper($generation).' - ';
		else
			$add_title	  = 'Audi '.strtoupper($model).' '.strtoupper($generation).' - Страница '.$page.' - ';
		
		$view 		  = new View('generation_tpl');
		
		$view->images = $images;
		$view->model  = $model;
		$view->generation = $generation;
		$view->page   = $page;
		$view->description = $description;
		$view->pages  = $pages;
		
		$this->print_template($add_title, $view);
	}
	
	public function action_get_image_by_model_and_id($model, $img)
	{
		$image = $this->audi->get_image($img);
		$images = $this->audi->get_near_images($img, 4);
		
		$tmp_images = $this->audi->get_near_images($img, 1);
		
		$link_images['left'] = null;
		$link_images['right'] = null;
		
		if(count($tmp_images) == 1)
		{
			if($tmp_images[0]['image_id'] < $img)
				$link_images['left'] = $tmp_images[0];
			else
				$link_images['right'] = $tmp_images[0];
		}
		else
		{
			$link_images['left'] = $tmp_images[0];
			$link_images['right'] = $tmp_images[1];
		}
		
		$add_title = $image['description'].' - Audi '.strtoupper($model).' - ';
		
		$view = new View('image_tpl');
		$view->image = $image;
		$view->images = $images;
		$view->link_images = $link_images;
		$view->model = $model;
		
		$this->print_template($add_title, $view);
	}
	
	public function action_get_image_by_generation_and_id($model, $generation, $img)
	{
		$image = $this->audi->get_image($img);
		$images = $this->audi->get_near_images($img, 4);
		
		$tmp_images = $this->audi->get_near_images($img, 1);
		
		$link_images['left'] = null;
		$link_images['right'] = null;
		
		if(count($tmp_images) == 1)
		{
			if($tmp_images[0]['image_id'] < $img)
				$link_images['left'] = $tmp_images[0];
			else
				$link_images['right'] = $tmp_images[0];
		}
		else
		{
			$link_images['left'] = $tmp_images[0];
			$link_images['right'] = $tmp_images[1];
		}
		
		$add_title = $image['description'].' - Audi '.strtoupper($model).' '.strtoupper($generation).' - ';
		
		$view = new View('image_tpl');
		$view->image = $image;
		$view->images = $images;
		$view->link_images = $link_images;
		$view->model = $model;
		$view->generation = $generation;
		
		$this->print_template($add_title, $view);
	}
}